package com.woniu.controller;

import com.woniu.entity.User;
import com.woniu.mapper.UserMapper;
import com.woniu.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @Classname UserController
 * @Description TODO
 * @Date 2025/8/5 17:15
 * @Created by pc
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;
    // @RequestMapping("/findAll")
    // public List<User> findAll(){
    //     return userService.findAll();
    // }

    @RequestMapping("/add")
    /* springMVC 框架可以帮我们自动绑定同名参数到形参上 ，并且进行类型转换*/
    // 200 成功  404 找不到资源 400 参数错误 405 请求方式不正确 500 代码错误
    public Integer addUser(@RequestParam(value = "username") String name,
                           @RequestParam(value = "age",required = false,defaultValue = "30") Integer age){
        System.out.println(name);
        age++;
        System.out.println(age);
        return 1;
    }

    @Resource
    private UserMapper userMapper;

    @RequestMapping("/add2")
    public Integer addUser2(@RequestBody User user){
        Integer i = userMapper.addUser(user);
        System.out.println(user);
        return i;
    }

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    /*
    * @RequestBody 表示接受json格式的请求参数
    * */
    public Map<String,Object> login(@RequestBody User user) throws Exception {
        return userService.login(user);
    }

}
